<template>
  <div class="xtx-infinite-loading" ref="target">
    <div class="loading" v-if="loading">
      <span class="img"></span>
      <span class="text">正在加载...</span>
    </div>
    <div class="none" v-if="finished">
      <span class="img"></span>
      <span class="text">亲，没有更多了</span>
    </div>
  </div>
</template>
<script setup>
import { useIntersectionObserver } from '@vueuse/core'

const props = defineProps({
  finished: {
    type: Boolean,
    default: false
  },
  loading: {
    type: Boolean,
    default: false
  }
})
const emit = defineEmits(['onload'])
// 监听是否进入可视去
const target = ref()
// 当target进入可视区,使用js原生方法intersectionobserver
useIntersectionObserver(target, ([{ isIntersecting }]) => { // 参1 目标dom
  if (isIntersecting) {
    if (!props.loading && !props.finished) {
      emit('onload') // 触发条件，1.加载完成，loading！=true  1.finished = false,
    }
  }
}, { // 配置选项
  threshold: 0 // 交叉比例
})
</script>
<script>
export default {
  name: 'XtxInfiniteLoading'
}
</script>
<style scoped lang='less'>
.xtx-infinite-loading {
  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .img {
      width: 50px;
      height: 50px;
      background: url(../../assets/images/load.gif) no-repeat center / contain;
    }
    .text {
      color: #999;
      font-size: 16px;
    }
  }
  .none {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .img {
      width: 200px;
      height: 134px;
      background: url(../../assets/images/none.png) no-repeat center / contain;
    }
    .text {
      color: #999;
      font-size: 16px;
    }
  }
}
</style>
